package com.crm.user.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONArray;
import com.crm.base.controller.BaseController;
import com.crm.customers.model.ApproveItem;
import com.crm.customers.service.ApproveItemService;
import com.crm.customers.service.ContractMoneylogService;
import com.crm.customers.service.ContractService;
import com.crm.sys.model.ConfigureItem;
import com.crm.sys.model.Department;
import com.crm.sys.service.SysService;
import com.crm.util.Pager;

/**
 * 财务管理controller
 * */
@Controller
public class MWorkController extends BaseController {
	
	@Autowired
	private ContractService contractService;
	
	@Autowired
	private ContractMoneylogService contractMoneylogService;
	@Autowired
	private ApproveItemService approveItemService;
	@Autowired
	private SysService sysService;
	 
	/**
	 * 进入审核列表页面
	 * @throws UnsupportedEncodingException 
	 * */
	@RequestMapping(value="/m/workmanage/approvelist{k}", produces="text/html;charset=UTF-8")
	public String approveList(@PathVariable String k,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		Integer pageNo = 1;
		Integer pageSize = 10;
		String keyWord = null;
		ApproveItem ai = new ApproveItem();
		if (k != null) {
			String key[] = k.split("_");
			for (String kk : key) {
				if (kk.indexOf("p") > -1) {
					pageNo = Integer.parseInt(kk.replace("p", ""));
				} else if (kk.indexOf("t") > -1) {
					ai.setType(Integer.parseInt(kk.replace("t", ""))); 
				}
				else if (kk.indexOf("s") > -1) {
					ai.setState(Integer.parseInt(kk.replace("s", ""))); 
				}
			}
		}
		if(request.getParameter("kwd")!=null){  
			keyWord = (URLDecoder.decode(URLDecoder.decode(request.getParameter("kwd"), "UTF-8"),"UTF-8"));
			ai.setKeyWord(keyWord);
		}
		ai.setApproveuser(getLoginUser().getId());
		if(ai.getState()==null){
			ai.setState(1);
		}
		List<ApproveItem>  list =approveItemService.getApproveItemAll(ai, pageNo, pageSize);
		Pager pager = new Pager(pageNo, pageSize);
		pager.setResultList(list);
		pager.setUrl("/workmanage/approvelist"+k);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("KeyWord",keyWord);
		params.put("state", ai.getState());
		params.put("type", ai.getType());
		pager.setParams(params);
		request.setAttribute("pager", pager); 
		return MOBILEPATH+"/workmanage/approvelist";	
	}
	
	/**
	 * 进入审核列表页面
	 * @throws UnsupportedEncodingException 
	 * */
	@RequestMapping(value="/m/workmanage/approvelistajax", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String approveListAjax(@ModelAttribute ApproveItem ai,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
	  
		ai.setApproveuser(getLoginUser().getId());
		if(ai.getState()==null){
			ai.setState(1);
		}
		List<ApproveItem>  list =approveItemService.getApproveItemAll(ai, getSkipToPage(), pageSize);
		List<Map> result = new ArrayList();
		for(ApproveItem aiobj:list){
			Map<String,Object> map = new HashMap();
			map.put("title", aiobj.getName());
			map.put("uname", aiobj.getUserInfo().getName());
			map.put("phone", aiobj.getUserInfo().getPhone());
			map.put("createdate", aiobj.getCreatedate().substring(0,10));
			Department   dep = sysService.getDepartmentById(aiobj.getUserInfo().getDeptid());
			String depName = "";
			if (dep != null) {
				depName =dep.getName();
			}  
			map.put("depname",depName);
			ConfigureItem   cf1 = sysService.getConfigureItemByValue(aiobj.getState().toString(),"approve_state");
			map.put("state",cf1.getName() );
			cf1 = sysService.getConfigureItemByValue(aiobj.getType().toString(),"approve_type");
			map.put("type", cf1.getName());
			result.add(map);
		}
		return JSONArray.toJSONString(result);
	}

}
